﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSharp.Core.Authorization.Resources;
using TSharp.Core.Mapping;
using TSharp.Core.Osgi;

[assembly: TSharp.Core.Osgi.RegExtensionPoint(typeof(ResourceCategoryRegistor))]
namespace TSharp.Core.Authorization.Resources
{  /// <summary>
    /// Class RegCategoryAttribute
    /// </summary>
    public class RegCategoryAttribute : RegExtensionAttribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RegCategoryAttribute"/> class.
        /// </summary>
        /// <param name="categoryType">Type of the category.</param>
        public RegCategoryAttribute(Type categoryType)
        {
            this.CategoryType = categoryType;
        }
        /// <summary>
        /// Gets the type of the category.
        /// </summary>
        /// <value>The type of the category.</value>
        public Type CategoryType { get; private set; }
    }

    /// <summary>
    /// 资源类别注册类
    /// </summary>
    public class ResourceCategoryRegistor : ExtensionPoint<RegCategoryAttribute>
    {
        private static readonly ConcurrentDictionary<string, ResourceCategory> resourceCategories =
            new ConcurrentDictionary<string, ResourceCategory>(CommonEqualityComparer.CurrentCultureIgnoreCase);

        /// <summary>
        /// 获取指定资源种类信息
        /// </summary>
        public static ResourceCategory GetResourceCategoryByCode(string categoryCode)
        {
            Assert.ArgumentNotNull(categoryCode, "categoryCode");
            ResourceCategory value;
            if (resourceCategories.TryGetValue(categoryCode, out value))
            {
                return value;
            }
            throw new KeyNotFoundException(string.Format("categoryCode：“{0}”资源类别未注册！", categoryCode));
        }
        /// <summary>
        /// 获取已注册的所有类别
        /// </summary>
        /// <returns>IEnumerable{ResourceCategory}.</returns>
        public static IEnumerable<ResourceCategory> GetAllResourceCategory()
        {
            return resourceCategories.Values;
        }
        /// <summary>
        /// 注册资源类别，OSGI实现
        /// </summary>
        protected override void Register(System.Reflection.Assembly assembly, RegCategoryAttribute attribute)
        {
            var resourceCategory = Activator.CreateInstance(attribute.CategoryType) as ResourceCategory;
            if (resourceCategory != null)
            {
                if (resourceCategories.ContainsKey(resourceCategory.code))
                    throw new TSharp.Core.Exceptions.CoreException("资源类别已注册。" + resourceCategory.code);
                resourceCategories.TryAdd(resourceCategory.code, resourceCategory);
            }
        }

        /// <summary>
        /// 注销资源类别
        /// </summary>
        protected override void UnRegister(System.Reflection.Assembly assembly, RegCategoryAttribute attribute)
        {
            var resourceCategory = Activator.CreateInstance(attribute.CategoryType) as ResourceCategory;
            ResourceCategory value;
            if (resourceCategory != null)
                resourceCategories.TryRemove(resourceCategory.code, out value);
        }
    }
}
